﻿using QLNet;
using FinPlusComponents;
using p = FinPlusAnalytics.QLConvParser;

namespace FinPlusAnalytics
{
    public class SwapRate : FinPlusComponent
    {
        public string Name { get; private set; }
        public RelinkableHandle<Quote> Rate { get; private set; }
        public RelinkableHandle<Quote> Spread { get; private set; }
        public RateHelper RateHelper { get; private set; }

        public SwapRate(string marketName, string name, double rate, double spread, string tenor, int fixingDays, int forwardStart, string floatLegIndex, string fixedLegFreq, string fixedLegDayCount, string fixedLegBizConv, string holidays, bool endOfMonth = true)
        {
            Name = name;
            var market = Markets.Instance.GetMarket(marketName);

            Rate = market.GetQuote(name);
            Rate.linkTo(new SimpleQuote(rate));

            //TODO
            Spread = new RelinkableHandle<Quote>(new SimpleQuote(spread));
            var indexTenor = floatLegIndex.Split('_');

            var index = market.GetIndex(floatLegIndex);
            RateHelper = new SwapRateHelper(Rate, p.GetPeriod(tenor), p.Calendar(holidays), p.Freq(fixedLegFreq), p.BizConv(fixedLegBizConv), p.DayCount(fixedLegDayCount),
               index, Spread, forwardStart * p.GetPeriod("days"));
            
            market.SetRateHelper(name, RateHelper);

        }
    }
}
